﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ChatRoomDA
/// </summary>
public class ChatRoomDA
{
	public ChatRoomDA()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public String Create(String title, String creatorID)
    {
        string command = "sp_tb_chatroom_CreateRoom";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@roomName", SqlDbType.NText, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@creatorID", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@roomName"].Value = title;
        sqlCmd.Parameters["@creatorID"].Value = creatorID;
        sqlCmd.ExecuteScalar();

        String result = sqlCmd.Parameters["@result"].Value.ToString();

        sqlCon.Close();

        //returns the id of the newly-created room
        return result;
    }

    public int Close(ChatRoom room)
    {
        string command = "sp_tb_chatroom_CloseRoom";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@roomID", SqlDbType.Int));
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@roomID"].Value = room.ID;

        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;
    }

    public String GetOtherUserInRoom(ChatRoom room,String currentUserID)
    {
        string command = "sp_tb_user_GetOtherUserInRoom2 @roomID='" + room.ID + "',@userID='" + currentUserID + "'";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        sqlCon.Open();

        SqlDataAdapter sda = new SqlDataAdapter(command, sqlCon);
        DataTable dsReminders = new DataTable();
        sda.Fill(dsReminders);

        return dsReminders.Rows[0]["userName"].ToString();
    }

    public Boolean StillHasMembers(ChatRoom room)
    {
        string command = "sp_tb_chatroom_CheckIfNotEmpty";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@roomID", SqlDbType.Int));
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@roomID"].Value = room.ID;

        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        if (result == 1)
            return true;
        return false;
    }
}
